<template>
  <div>
    <div>
      <el-input
        size="small"
        class="addDepartInput"
        placeholder="请输入需要添加的学院名称"
        prefix-icon="el-icon-plus"
        @keydown.enter.native="addDepart"
        v-model="de.name">
      </el-input>
      <el-button icon="el-icon-plus" size="small" type="primary" @click="addDepart">新增</el-button>
    </div>
    <div class="departMain">
      <el-table
        :data="departments"
        border
        stripe
        size="small"
        style="width: 70%">
        <el-table-column prop="id" label="编号" width="50" align="center"></el-table-column>
        <el-table-column prop="name" label="学院名称"  >
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" ></el-table-column>
        <el-table-column prop="enabled"
                         label="是否启用"
                         >
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enabled" type="success">已启用</el-tag>
            <el-tag v-else type="danger">未启用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="showEditView(scope.$index,scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.$index,scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <el-dialog title="编辑学院信息" :visible.sync="dialogVisible" width="30%">
      <div>
        <table>
        <tr>
          <td>
            <el-tag>学院名称</el-tag>
          </td>
          <td>
        <el-input v-model="updateDepartment.name" size="small" class="updateDepartInput"></el-input>
          </td>
        </tr>
          <tr>
            <td>
              <el-tag>是否启用</el-tag>
            </td>
            <td>
              <el-switch
                  v-model="updateDepartment.enabled"
                  activecolor="#13ce66"
                  inactive-color="#ff4949"
                  active-text="启用"
                  inactive-text="禁用">
              </el-switch>
            </td>
          </tr>
        </table>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="dialogVisible = false">取消</el-button>
        <el-button size="small" type="primary" @click="doUpdate">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: "deSet",
    data() {
      return {
        de: {//前端传值 后端方法的参数是name
          name: ''
        },
        dialogVisible: false,
        updateDepartment: {
          name: '',
          enabled:false
        },
        departments: []
      }
    },
    mounted() {
      this.initDeparts();
    },
    methods: {

      addDepart() {
        //添加专业
        if (this.de.name) {
          this.postRequest('/system/basic/depart/', this.de).then(resp => {
            if (resp) {
              this.initDeparts();
              this.de.name = '';
            }
          })
        } else {
          this.$message.error("学院名称不可以为空！");
        }
      },
      showEditView(index, data) {
        //打开编辑页面
        Object.assign(this.updateDepartment, data);//阻止同步更新
        this.updateDepartment.createDate = '';
        this.dialogVisible = true;
      },
      doUpdate() {
        //修改学院信息
        this.putRequest('/system/basic/depart/', this.updateDepartment).then(resp => {
          if (resp) {
            this.initDeparts();
            this.updateDepartment.name = '';
            this.dialogVisible = false;
          }
        })
      },
      handleDelete(index, data) {
        //删除专业
        this.$confirm('此操作将永久删除该【' + data.name + '】, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.deleteRequest('/system/basic/depart/' + data.id).then(resp => {
            if (resp) {
              this.initDeparts();
            }
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      initDeparts() {
        //初始化数据
        this.getRequest('/system/basic/depart/').then(resp => {
          if (resp) {
            this.departments = resp;
          }
        })
      }
    }
  }
</script>

<style scoped>

  .addDepartInput {
    width: 300px;
    margin-right: 8px;
  }

  .departMain {
    margin-top: 10px;
  }

  .updateDepartInput {
    width: 200px;
    margin-left: 8px;
  }
</style>
